package com.fand.platform.configuration.service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import com.fand.dto.PaginationDto;
import com.fand.platform.configuration.dao.DictionaryDao;
import com.fand.platform.configuration.dto.DictionaryTyDto;
import com.fand.platform.configuration.dto.DictionaryDto;
import com.fand.platform.configuration.dto.DictionaryLaDto;

/**
 * 字典服务
 *
 * @author yaoHT
 * 
 */
@Getter 
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class DictionaryDefaultService implements DictionaryService {
	private DictionaryDao dictionaryDao;
	
	@Override
	@CacheEvict(value = DictionaryService.CACHE_NAME, allEntries = true)
	public void reload() {
	}

	@Override
	@Transactional
	public DictionaryTyDto saveDictionaryTyByIdOrCodeForDictionaryTy(DictionaryTyDto saveDictionaryTy) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveDictionaryTy.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		DictionaryTyDto oldDictionaryTy = this.dictionaryDao.selectDictionaryTyBy1ForMappedObject(DictionaryTyDto.class,
				saveDictionaryTy, null, whereEqProperties);

		if (oldDictionaryTy == null) {
			this.dictionaryDao.insertDictionaryTyBy1(saveDictionaryTy);

			return saveDictionaryTy;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveDictionaryTy, oldDictionaryTy, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.dictionaryDao.updateDictionaryTyBy1(saveDictionaryTy, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveDictionaryTy, oldDictionaryTy, updateProperties);
		}

		return oldDictionaryTy;
	}

	@Override
	@Transactional
	public DictionaryTyDto saveDictionaryTyBy2ForDictionaryTy(DictionaryTyDto saveDictionaryTy) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveDictionaryTy.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		DictionaryTyDto oldDictionaryTy = this.dictionaryDao.selectDictionaryTyBy1ForMappedObject(DictionaryTyDto.class,
				saveDictionaryTy, null, whereEqProperties);

		if (oldDictionaryTy == null) {
			this.dictionaryDao.insertDictionaryTyBy1(saveDictionaryTy, new String[] {});

			return saveDictionaryTy;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveDictionaryTy, oldDictionaryTy, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.dictionaryDao.updateDictionaryTyBy1(saveDictionaryTy, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveDictionaryTy, oldDictionaryTy, updateProperties);
		}

		return oldDictionaryTy;
	}

	@Override
	@Transactional
	public int deleteDictionaryTyBy1(DictionaryTyDto deleteDictionaryTy) {
		return this.dictionaryDao.deleteDictionaryTyBy1(deleteDictionaryTy, new String[] {});
	}

	@Override
	@Transactional
	public int deleteDictionaryTyByIds(DictionaryTyDto deleteDictionaryTy) {
		return this.dictionaryDao.deleteDictionaryTyByIds(deleteDictionaryTy);
	}

	@Override
	@Transactional
	public int setDictionaryTyBy1(DictionaryTyDto setDictionaryTy) {
		return this.dictionaryDao.updateDictionaryTyBy1(setDictionaryTy, new String[] {}, new String[] {});
	}

	@Override
	@Transactional
	public int setDictionaryTyBy2(DictionaryTyDto setDictionaryTy) {
		return this.dictionaryDao.updateDictionaryTyBy2(setDictionaryTy);
	}

	@Override
	public Integer readDictionaryTyByCodeNqIdForInteger(DictionaryTyDto readDictionaryTy) {
		return this.dictionaryDao.selectDictionaryTyByCodeNqIdForInteger(readDictionaryTy);
	}

	@Override
	public Integer readDictionaryTyBy2ForInteger(DictionaryTyDto readDictionaryTy) {
		return this.dictionaryDao.selectDictionaryTyBy2ForObject(Integer.class, readDictionaryTy, null);
	}

	@Override
	public List<Integer> readListDictionaryTyBy1(DictionaryTyDto readDictionaryTy) {
		return this.dictionaryDao.selectDictionaryTyBy1ForObjectList(Integer.class, readDictionaryTy, null,
				new String[] {});
	}

	@Override
	public List<Integer> readListDictionaryTyBy2(DictionaryTyDto readDictionaryTy) {
		return this.dictionaryDao.selectDictionaryTyBy2ForObjectList(Integer.class, readDictionaryTy, null);
	}

	@Override
	public DictionaryTyDto getDictionaryTyBy1(DictionaryTyDto getDictionaryTy) {
		return this.dictionaryDao.selectDictionaryTyBy1ForMappedObject(DictionaryTyDto.class, getDictionaryTy, null, "id");
	}

	@Override
	public DictionaryTyDto getDictionaryTyById(DictionaryTyDto getDictionaryTy) {
		return this.dictionaryDao.selectDictionaryTyBy1ForMappedObject(DictionaryTyDto.class, getDictionaryTy, null, "id");
	}

	@Override
	public List<DictionaryTyDto> findDictionaryTyBy1(DictionaryTyDto findDictionaryTy) {
		return this.dictionaryDao.selectDictionaryTyBy1ForMappedObjectList(DictionaryTyDto.class, findDictionaryTy,
				new String[] {}, new String[] {});
	}

	@Override
	public List<DictionaryTyDto> findDictionaryTyBy2(DictionaryTyDto findDictionaryTy) {
		return this.dictionaryDao.selectDictionaryTyBy2ForMappedObjectList(DictionaryTyDto.class, findDictionaryTy);
	}

	@Override
	public PaginationDto<DictionaryTyDto> paginationDictionaryTyBy1ForDictionaryTyList(DictionaryTyDto paginationDictionaryTy) {
		return this.dictionaryDao.paginationDictionaryTyBy1ForMappedObjectList(DictionaryTyDto.class,
				paginationDictionaryTy);
	}

	@Override
	public PaginationDto<Map<String, Object>> paginationDictionaryTyBy1ForMapList(DictionaryTyDto paginationDictionaryTy) {
		return this.dictionaryDao.paginationDictionaryTyBy1ForMapList(paginationDictionaryTy);
	}

	@Override
	@Transactional
	public DictionaryDto saveDictionaryByIdOrCodeForDictionary(DictionaryDto saveDictionary) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveDictionary.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		DictionaryDto oldDictionary = this.dictionaryDao.selectDictionaryBy1ForMappedObject(DictionaryDto.class,
				saveDictionary, null, whereEqProperties);

		if (oldDictionary == null) {
			this.dictionaryDao.insertDictionaryBy1(saveDictionary);

			return saveDictionary;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveDictionary, oldDictionary, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.dictionaryDao.updateDictionaryBy1(saveDictionary, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveDictionary, oldDictionary, updateProperties);
		}

		return oldDictionary;
	}

	@Override
	@Transactional
	public DictionaryDto saveDictionaryBy2ForDictionary(DictionaryDto saveDictionary) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveDictionary.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		DictionaryDto oldDictionary = this.dictionaryDao.selectDictionaryBy1ForMappedObject(DictionaryDto.class,
				saveDictionary, null, whereEqProperties);

		if (oldDictionary == null) {
			this.dictionaryDao.insertDictionaryBy1(saveDictionary, new String[] {});

			return saveDictionary;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveDictionary, oldDictionary, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.dictionaryDao.updateDictionaryBy1(saveDictionary, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveDictionary, oldDictionary, updateProperties);
		}

		return oldDictionary;
	}

	@Override
	@Transactional
	public int deleteDictionaryBy1(DictionaryDto deleteDictionary) {
		return this.dictionaryDao.deleteDictionaryBy1(deleteDictionary, new String[] {});
	}

	@Override
	@Transactional
	public int deleteDictionaryByIds(DictionaryDto deleteDictionary) {
		return this.dictionaryDao.deleteDictionaryByIds(deleteDictionary);
	}

	@Override
	@Transactional
	public int setDictionaryBy1(DictionaryDto setDictionary) {
		return this.dictionaryDao.updateDictionaryBy1(setDictionary, new String[] {}, new String[] {});
	}

	@Override
	@Transactional
	public int setDictionaryBy2(DictionaryDto setDictionary) {
		return this.dictionaryDao.updateDictionaryBy2(setDictionary);
	}

	@Override
	public Integer readDictionaryByCodeNqIdForInteger(DictionaryDto readDictionary) {
		return this.dictionaryDao.selectDictionaryByCodeNqIdForInteger(readDictionary);
	}

	@Override
	public Integer readDictionaryBy2ForInteger(DictionaryDto readDictionary) {
		return this.dictionaryDao.selectDictionaryBy2ForObject(Integer.class, readDictionary, null);
	}

	@Override
	public List<Integer> readListDictionaryBy1(DictionaryDto readDictionary) {
		return this.dictionaryDao.selectDictionaryBy1ForObjectList(Integer.class, readDictionary, null,
				new String[] {});
	}

	@Override
	public List<Integer> readListDictionaryBy2(DictionaryDto readDictionary) {
		return this.dictionaryDao.selectDictionaryBy2ForObjectList(Integer.class, readDictionary, null);
	}

	@Override
	public DictionaryDto getDictionaryBy1(DictionaryDto getDictionary) {
		return this.dictionaryDao.selectDictionaryBy1ForMappedObject(DictionaryDto.class, getDictionary, null, "id");
	}

	@Override
	public DictionaryDto getDictionaryById(DictionaryDto getDictionary) {
		return this.dictionaryDao.selectDictionaryBy1ForMappedObject(DictionaryDto.class, getDictionary, null, "id");
	}

	@Override
	public List<DictionaryDto> findDictionaryBy1(DictionaryDto findDictionary) {
		return this.dictionaryDao.selectDictionaryBy1ForMappedObjectList(DictionaryDto.class, findDictionary,
				new String[] {}, new String[] {});
	}

	@Override
	public List<DictionaryDto> findDictionaryBy2(DictionaryDto findDictionary) {
		return this.dictionaryDao.selectDictionaryBy2ForMappedObjectList(DictionaryDto.class, findDictionary);
	}

	@Override
	public PaginationDto<DictionaryDto> paginationDictionaryBy1ForDictionaryList(DictionaryDto paginationDictionary) {
		return this.dictionaryDao.paginationDictionaryBy1ForMappedObjectList(DictionaryDto.class,
				paginationDictionary);
	}

	@Override
	public PaginationDto<Map<String, Object>> paginationDictionaryBy1ForMapList(DictionaryDto paginationDictionary) {
		return this.dictionaryDao.paginationDictionaryBy1ForMapList(paginationDictionary);
	}

	@Override
	@Transactional
	public DictionaryLaDto saveDictionaryLaByIdOrCodeForDictionaryLa(DictionaryLaDto saveDictionaryLa) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveDictionaryLa.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		DictionaryLaDto oldDictionaryLa = this.dictionaryDao.selectDictionaryLaBy1ForMappedObject(DictionaryLaDto.class,
				saveDictionaryLa, null, whereEqProperties);

		if (oldDictionaryLa == null) {
			this.dictionaryDao.insertDictionaryLaBy1(saveDictionaryLa);

			return saveDictionaryLa;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveDictionaryLa, oldDictionaryLa, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.dictionaryDao.updateDictionaryLaBy1(saveDictionaryLa, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveDictionaryLa, oldDictionaryLa, updateProperties);
		}

		return oldDictionaryLa;
	}

	@Override
	@Transactional
	public DictionaryLaDto saveDictionaryLaBy2ForDictionaryLa(DictionaryLaDto saveDictionaryLa) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(saveDictionaryLa.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		DictionaryLaDto oldDictionaryLa = this.dictionaryDao.selectDictionaryLaBy1ForMappedObject(DictionaryLaDto.class,
				saveDictionaryLa, null, whereEqProperties);

		if (oldDictionaryLa == null) {
			this.dictionaryDao.insertDictionaryLaBy1(saveDictionaryLa, new String[] {});

			return saveDictionaryLa;
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(saveDictionaryLa, oldDictionaryLa, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.dictionaryDao.updateDictionaryLaBy1(saveDictionaryLa, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(saveDictionaryLa, oldDictionaryLa, updateProperties);
		}

		return oldDictionaryLa;
	}

	@Override
	@Transactional
	public int deleteDictionaryLaBy1(DictionaryLaDto deleteDictionaryLa) {
		return this.dictionaryDao.deleteDictionaryLaBy1(deleteDictionaryLa, new String[] {});
	}

	@Override
	@Transactional
	public int deleteDictionaryLaByIds(DictionaryLaDto deleteDictionaryLa) {
		return this.dictionaryDao.deleteDictionaryLaByIds(deleteDictionaryLa);
	}

	@Override
	@Transactional
	public int setDictionaryLaBy1(DictionaryLaDto setDictionaryLa) {
		return this.dictionaryDao.updateDictionaryLaBy1(setDictionaryLa, new String[] {}, new String[] {});
	}

	@Override
	@Transactional
	public int setDictionaryLaBy2(DictionaryLaDto setDictionaryLa) {
		return this.dictionaryDao.updateDictionaryLaBy2(setDictionaryLa);
	}

	@Override
	public Integer readDictionaryLaByCodeNqIdForInteger(DictionaryLaDto readDictionaryLa) {
		return this.dictionaryDao.selectDictionaryLaByCodeNqIdForInteger(readDictionaryLa);
	}

	@Override
	public Integer readDictionaryLaBy2ForInteger(DictionaryLaDto readDictionaryLa) {
		return this.dictionaryDao.selectDictionaryLaBy2ForObject(Integer.class, readDictionaryLa, null);
	}

	@Override
	public List<Integer> readListDictionaryLaBy1(DictionaryLaDto readDictionaryLa) {
		return this.dictionaryDao.selectDictionaryLaBy1ForObjectList(Integer.class, readDictionaryLa, null,
				new String[] {});
	}

	@Override
	public List<Integer> readListDictionaryLaBy2(DictionaryLaDto readDictionaryLa) {
		return this.dictionaryDao.selectDictionaryLaBy2ForObjectList(Integer.class, readDictionaryLa, null);
	}

	@Override
	public DictionaryLaDto getDictionaryLaBy1(DictionaryLaDto getDictionaryLa) {
		return this.dictionaryDao.selectDictionaryLaBy1ForMappedObject(DictionaryLaDto.class, getDictionaryLa, null, "id");
	}

	@Override
	public DictionaryLaDto getDictionaryLaById(DictionaryLaDto getDictionaryLa) {
		return this.dictionaryDao.selectDictionaryLaBy1ForMappedObject(DictionaryLaDto.class, getDictionaryLa, null, "id");
	}

	@Override
	public List<DictionaryLaDto> findDictionaryLaBy1(DictionaryLaDto findDictionaryLa) {
		return this.dictionaryDao.selectDictionaryLaBy1ForMappedObjectList(DictionaryLaDto.class, findDictionaryLa,
				new String[] {}, new String[] {});
	}

	@Override
	public List<DictionaryLaDto> findDictionaryLaBy2(DictionaryLaDto findDictionaryLa) {
		return this.dictionaryDao.selectDictionaryLaBy2ForMappedObjectList(DictionaryLaDto.class, findDictionaryLa);
	}

	@Override
	public PaginationDto<DictionaryLaDto> paginationDictionaryLaBy1ForDictionaryLaList(DictionaryLaDto paginationDictionaryLa) {
		return this.dictionaryDao.paginationDictionaryLaBy1ForMappedObjectList(DictionaryLaDto.class,
				paginationDictionaryLa);
	}

	@Override
	public PaginationDto<Map<String, Object>> paginationDictionaryLaBy1ForMapList(DictionaryLaDto paginationDictionaryLa) {
		return this.dictionaryDao.paginationDictionaryLaBy1ForMapList(paginationDictionaryLa);
	}
}